package com.hy.hongying.service.impl;

import com.hy.hongying.config.SystemConfig;
import com.hy.hongying.dao.OssDao;
import com.hy.hongying.entity.Oss;
import com.hy.hongying.service.intf.OssService;
import com.hy.hongying.third.AliOssUtil;
import com.hy.hongying.util.DateUtil;
import com.hy.hongying.util.FileUtil;
import com.hy.hongying.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@Service
public class OssServiceImpl implements OssService {
    @Autowired
    private OssDao odao;
    @Override
    public R insertOss(MultipartFile file,String token,Integer type) {
        if (token==null||token.length()==0){
            return R.fail("亲你还没有登录");
        }
        if (!file.isEmpty()){
            Date date= DateUtil.getDate(SystemConfig.OSS_URL_DAYS);//得到具体到期的天数
            String rname= FileUtil.rename(file.getOriginalFilename());
            try {
                String upload = AliOssUtil.upload(rname, date, file.getInputStream());
                if (upload!=null&&upload.length()>0){
                    Oss oss = new Oss(AliOssUtil.bucket,type, upload, date, new Date(),rname);
                    odao.insert(oss);
                    return R.ok(oss.getId());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return R.fail("亲这里不能为空哦");
    }
}
